
public class YesterDate {
    public static String getYesterDate(int year, int month, int day){
        if (!isValidDate(year, month, day)) {
            throw new IllegalArgumentException("Invalid date");
        }
        if(day>1){
            day--;
        }
        else{
            if(month>1) {
                month--;
                day = getDaysInMonth(year, month);;
            }
            else {
                year--;
                month=12;
                day=31;
            }
        }
        return String.format("%04d-%02d-%02d", year, month, day);
    }
    private static boolean isValidDate(int year, int month, int day){
        if(year<1900||year>2050||month<1||month>12||day<1||day>31) {
            return false;
        }
        return day <= getDaysInMonth(year, month);
    }
    private static int getDaysInMonth(int year ,int month){
        switch (month){
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                return 31;
            case 2:
                return isLeapYear(year) ? 29 : 28;
            default:
                return 30;
        }
    }
    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}
